package com.huadi.qi.controller;

import com.huadi.qi.bean.News;
import com.huadi.qi.bean.User;
import com.huadi.qi.mapper.NewsMapper;
import com.huadi.qi.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
public class NewsController {
    @Autowired
    NewsMapper newsMapper;
    @Autowired
    NewsService newsService;

    //新闻
    @GetMapping("/news")
    public String news(Model model){
        List<News> list = newsService.list();
        model.addAttribute("news", list);
        return "news";
    }
    //新闻主页
    @GetMapping("/newMain{id}")
    public String newMain(@PathVariable("id") Integer id, Model model ){
        News news = newsMapper.selectById(id);
        model.addAttribute("m",news);
        return "newMain";
    }
    //管理员端
    @GetMapping("/news/list")
    public String list(@RequestParam(defaultValue = "") String summary,Model model){
        List<News> list = new ArrayList<>();
        if (summary.equals("")){
            list = newsService.list();
            model.addAttribute("news",list);
        }else {
            list = newsMapper.selectSummary(summary);
            model.addAttribute("selectMsg","查询成功");
            model.addAttribute("news",list);
        }
        return "/admin/news/list";
    }
    //删除
    @GetMapping("/news/del{id}")
    public String del(@PathVariable("id") String id, HttpSession session){
        boolean b = newsService.removeById(id);
        if (b == true){
            session.setAttribute("NewMsg","删除成功！");
            return "redirect:/news/list";
        }
            session.setAttribute("NewMsg","删除失败！");
            return "redirect:/news/list";
    }

    //添加新闻
    @GetMapping("/news/add")
    public String add(){
        return "/admin/news/add";
    }
    @PostMapping("/news/add")
    public String postAdd(String summary, String content, MultipartFile icon,
                          String media, String time, MultipartFile picture, HttpSession session)throws IOException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date data = null;
        //图片服务器
        if (!icon.isEmpty()){
            String originalFilename = icon.getOriginalFilename();
        }
        if (!picture.isEmpty()){
            String originalFilename = picture.getOriginalFilename();
//            String h = originalFilename.substring(originalFilename.length()-4);
        }
        Random random = new Random();
        int q = random.nextInt(80000);
        int p = random.nextInt(100000);
        String h = ".jpg";
        String iconFilename = q+h;
        String pictureFilename = p+h;
        icon.transferTo(new File("F:\\华迪培训资料\\项目\\qi\\src\\main\\resources\\static\\img\\news\\"+iconFilename));
        picture.transferTo(new File("F:\\华迪培训资料\\项目\\qi\\src\\main\\resources\\static\\img\\news\\"+pictureFilename));
        try {
            data = sdf.parse(time);
            News news = new News();
            news.setSummary(summary);
            news.setContent(content);
            news.setMedia(media);
            news.setTime(data);
            news.setIcon(iconFilename);
            news.setPicture(pictureFilename);
            newsMapper.insert(news);
            session.setAttribute("NewMsg","添加成功");
            return "redirect:/news/list";
        }catch (Exception e){
            session.setAttribute("NewMsg","添加失败");
            return "redirect:/news/list";
        }
    }

    //修改新闻
    @GetMapping("/news/update{id}")
    public String update(@PathVariable("id") Integer id, Model model){
        News byId = newsService.getById(id);
        model.addAttribute("news",byId);
        return "/admin/news/update";
    }
    @PostMapping("/news/update")
    public String postupdate(Integer id, String summary, String content, MultipartFile icon,
                          String media, String time, MultipartFile picture, HttpSession session)throws IOException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date data = null;
        //图片服务器
        if (!icon.isEmpty()){
            String originalFilename = icon.getOriginalFilename();
        }
        if (!picture.isEmpty()){
            String originalFilename = picture.getOriginalFilename();
//            String h = originalFilename.substring(originalFilename.length()-4);
        }
        Random random = new Random();
        int q = random.nextInt(80000);
        int p = random.nextInt(100000);
        String h = ".jpg";
        String iconFilename = q+h;
        String pictureFilename = p+h;
        icon.transferTo(new File("F:\\华迪培训资料\\项目\\qi\\src\\main\\resources\\static\\img\\news\\"+iconFilename));
        picture.transferTo(new File("F:\\华迪培训资料\\项目\\qi\\src\\main\\resources\\static\\img\\news\\"+pictureFilename));
        try {
            data = sdf.parse(time);
            News news = new News();
            news.setId(id);
            news.setSummary(summary);
            news.setContent(content);
            news.setMedia(media);
            news.setTime(data);
            news.setIcon(iconFilename);
            news.setPicture(pictureFilename);
            newsMapper.updateById(news);
            session.setAttribute("NewMsg","修改成功");
            return "redirect:/news/list";
        }catch (Exception e){
            session.setAttribute("NewMsg","修改失败");
            return "redirect:/news/list";
        }
    }


}
